package com.fireant.ommonadapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.Spanned;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * ViewHolder
 * Created by zhangdeyi on 15/9/20.
 */
public class ViewHolder {

    private View mConvertView;
    private SparseArray<View> mViews;
    private Context mContext;
    private int mLayoutId;
    private int mPosition;

    private ViewHolder(Context context, ViewGroup viewGroup, int layoutId, int position) {
        this.mViews = new SparseArray<>();
        this.mContext = context;
        this.mLayoutId = layoutId;
        this.mPosition = position;
        this.mConvertView = LayoutInflater.from(context).inflate(layoutId, viewGroup, false);
        mConvertView.setTag(this);
    }

    public static ViewHolder get(Context context, View convertView, ViewGroup parent,
                                 int layoutId, int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        } else {
            ViewHolder vh = (ViewHolder) convertView.getTag();
            vh.mPosition = position;
            return vh;
        }
    }

    public int getPosition() {
        return this.mPosition;
    }

    public int getLayoutId() {
        return this.mLayoutId;
    }
    
    public View getConvertView() {
        return this.mConvertView;
    }

    /***
     * 获取view
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public ViewHolder setVisible(int viewId, boolean isVisible) {
        View view = getView(viewId);
        view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        return this;
    }

    /** 设置文字start **/

    public ViewHolder setText(int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
        return this;
    }

    public ViewHolder setText(int viewId, CharSequence charSequence) {
        TextView textView = getView(viewId);
        textView.setText(charSequence);
        return this;
    }

    public ViewHolder setText(int viewId, Spanned spanned) {
        TextView textView = getView(viewId);
        textView.setText(spanned);
        return this;
    }

    public ViewHolder setText(int viewId, int textRes) {
        TextView textView = getView(viewId);
        textView.setText(textRes);
        return this;
    }

    /** 设置文字end **/

    /** 设置图片start **/

    public ViewHolder setImageResource(int viewId, int res) {
        ImageView imageView = getView(viewId);
        imageView.setImageResource(viewId);
        return this;
    }

    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView imageView = getView(viewId);
        imageView.setImageBitmap(bitmap);
        return this;
    }

    public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView imageView = getView(viewId);
        imageView.setImageDrawable(drawable);
        return this;
    }

    /** 设置图片end **/
}
